package com.moshang.blog.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.moshang.blog.core.constant.Operation;
import com.moshang.blog.entity.TbSort;
import com.moshang.blog.core.constant.MySysUser;
import com.moshang.blog.mapper.TbSortMapper;
import com.moshang.blog.service.TbSortService;
import com.moshang.blog.core.utils.TreeUtil;
import com.moshang.blog.core.utils.ZtreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: blog
 * @description: ${description}
 * @author: xieweiwei
 * @create: 2018-11-16 09:42
 **/
@Service
public class TbSortServiceImpl extends ServiceImpl<TbSortMapper, TbSort> implements TbSortService {
    @Resource
    private  TbSortMapper tbSortMapper;
    @Override
    public List<TbSort> selectAllSorts(Map<String, Object> map) {
        return tbSortMapper.selectAllSorts(map);
    }

    @Override
    public int getCountByName(String name) {
        QueryWrapper<TbSort> wrapper = new QueryWrapper<>();
        wrapper.eq("is_activited",0);
        wrapper.eq("name",name);
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public void saveOrUpdateSort(TbSort tbSort, String type) {
        if (Operation.INSERT.equals(type)) {

            tbSort.setCreateBy(MySysUser.currentUser().getId());
            tbSort.setUpdateBy(MySysUser.currentUser().getId());
            tbSort.setCreateTime(new Date());
            tbSort.setUpdateTime(new Date());
            tbSort.setCreateName(MySysUser.userName());
            tbSort.setUpdateName(MySysUser.userName());
            tbSort.setIsActivited(0);
        }else {
            tbSort.setUpdateBy(MySysUser.currentUser().getId());
            tbSort.setUpdateName(MySysUser.userName());
        }
        saveOrUpdate(tbSort);
    }

    @Override
    public List<TbSort> selectByParent(Integer id) {
        List<TbSort> tbSortList=new ArrayList<>();
        QueryWrapper<TbSort> entityWrapper=new QueryWrapper<>();
        entityWrapper.eq("id",id);
        List<TbSort> list=baseMapper.selectList(entityWrapper);
        for (TbSort tbSort:list){
            tbSortList.add(tbSort);
            if (tbSort.getParentId()!=0){
                getChids(tbSortList,tbSort.getId());
            }
        }
        return tbSortList;
    }



    @Override
    public List<ZtreeUtil> selectZtreeData() {
        Map<String,Object> map = Maps.newHashMap();
        map.put("parentId",null);
        return baseMapper.selectZtreeData(map);
    }

    @Override
    public JSONArray getTreeUtil(Object o) {
        TreeUtil treeUtil = null;
        QueryWrapper<TbSort> entity = new QueryWrapper<TbSort>();
        List<TbSort> tbSorts = baseMapper.selectList(entity);
        List<TbSort> supers = tbSorts.stream().filter(tbSort ->
                tbSort.getParentId()==0)
                .collect(Collectors.toList());
        tbSorts.removeAll(supers);
        supers.sort(Comparator.comparingInt(TbSort::getSort));
        JSONArray jsonArr = new JSONArray();
        for (TbSort tbSort : supers) {
            treeUtil = getChildByTree(tbSort, tbSorts, 0, 0);
            jsonArr.add(treeUtil);
        }
        return jsonArr;
    }

    @Override
    public List<TbSort> getSortByParent(Integer parentId) {
        QueryWrapper<TbSort> entityWrapper=new QueryWrapper<>();
        entityWrapper.eq("parent_Id",parentId);
        entityWrapper.eq("is_activited",0);
        entityWrapper.orderByAsc("sort");
        List<TbSort> list=baseMapper.selectList(entityWrapper);
        return list;
    }

    @Override
    public List<TbSort> getSorts() {
        QueryWrapper<TbSort> entityWrapper=new QueryWrapper<>();
        entityWrapper.eq("is_activited",0);
        entityWrapper.orderByAsc("sort");
        List<TbSort> list=baseMapper.selectList(entityWrapper);
        return list;
    }

    private TreeUtil getChildByTree(TbSort tbSort, List<TbSort> tbSorts,int layer, Integer pid ) {
        layer++;
        TreeUtil treeUtil = new TreeUtil();
        List<TbSort> childTbsort = tbSorts.stream().filter(s ->
                s.getParentId()==tbSort.getId()).collect(Collectors.toList());
        tbSorts.removeAll(childTbsort);
        treeUtil.setId(tbSort.getId().toString());
        treeUtil.setName(tbSort.getName());
        treeUtil.setLayer(layer);
        if (pid!=null){
            treeUtil.setId(pid.toString());
        }
          if (childTbsort!=null) {
              for (TbSort tbSort1 : childTbsort) {
                  TreeUtil m = getChildByTree(tbSort1, tbSorts, layer, tbSort1.getId());
                  if (m!=null) {

                      treeUtil.getChildren().add(m);
                  }
              }
          }
        return treeUtil;
    }

    public List<TbSort> getChids(List tbSortList,Integer parentId){
        QueryWrapper<TbSort> entityWrapper=new QueryWrapper<>();
        entityWrapper.eq("parent_id",parentId);
        List<TbSort> list=baseMapper.selectList(entityWrapper);
        for (TbSort tbSort:list){
            tbSortList.add(tbSort);
            if (parentId!=0){
                getChids(tbSortList,tbSort.getId());
            }
        }
        return tbSortList;
    }

}
